package com.dkm.nettysocketdemo.myWebsite.service.impl;

import com.dkm.nettysocketdemo.myWebsite.dao.HelmetFeedbackMapper;
import com.dkm.nettysocketdemo.myWebsite.domain.HelmetFeedback;
import com.dkm.nettysocketdemo.myWebsite.service.HelmetFeedbackService;
import com.dkm.nettysocketdemo.untils.FileTools;
import com.dkm.nettysocketdemo.untils.redis.RedisOperator;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
@Transactional(rollbackFor = Exception.class)
public class HelmetFeedbackServiceImpl implements HelmetFeedbackService {

    /**
     * 反馈Mapper
     */
    @Resource
    HelmetFeedbackMapper helmetFeedbackMapper;

    /**
     * 文件上传工具类
     */
    @Resource
    FileTools fileTools;

    /**
     * redis 工具类
     */
    @Resource
    RedisOperator redisOperator;

    Gson gson = new Gson();
    /**
     * id查询
     * @param pkFeedBackId 反馈id
     * @return 反馈对象
     */
    @Override
    public HelmetFeedback findById(String pkFeedBackId) {
        HelmetFeedback helmetFeedback=gson.fromJson((String) redisOperator.get(pkFeedBackId),HelmetFeedback.class) ;
        if (ObjectUtils.isEmpty(helmetFeedback)){
            helmetFeedback = helmetFeedbackMapper.selectByPrimaryKey(pkFeedBackId);
            redisOperator.set(pkFeedBackId,gson.toJson(helmetFeedback));
        }
        return helmetFeedback ;
    }

    /**
     * 新增反馈对象
     * @param helmetFeedback 反馈对象
     * @return 回调函数
     */
    @Override
    public int insertHelmetFeedback(HelmetFeedback helmetFeedback) {
        helmetFeedback.setPkFeedbackId(UUID.randomUUID().toString().replaceAll("-",""));
        helmetFeedback.setIsRevision("0");
        int row = helmetFeedbackMapper.insertSelective(helmetFeedback);
        if (row > 0){
            redisOperator.set(helmetFeedback.getPkFeedbackId(),gson.toJson(helmetFeedback));
        }
        return row;
    }

    /**
     * id删除
     * @param pkFeedBack 反馈id
     * @return 回调函数
     */
    @Override
    public int deleteById(String pkFeedBack) {
        int row = helmetFeedbackMapper.deleteByPrimaryKey(pkFeedBack);
        if(row > 0){
            redisOperator.del(pkFeedBack);
        }
        return row;
    }

    /**
     * 修改
     * @param helmetFeedback 反馈对象
     * @return 回调函数
     */
    @Override
    public int updateHelmetFeedback(HelmetFeedback helmetFeedback) {
        int row = helmetFeedbackMapper.updateByPrimaryKeySelective(helmetFeedback);
        if(row > 0){
            HelmetFeedback feedback = helmetFeedbackMapper.selectByPrimaryKey(helmetFeedback.getPkFeedbackId());
            redisOperator.set(helmetFeedback.getPkFeedbackId(),gson.toJson(feedback));
        }
        return row;
    }

    /**
     * 批量查询反馈对象
     * @param helmetFeedback 反馈对象
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param page 当前页数
     * @param limit 显示行数
     * @return 查询结果
     */
    @Override
    public List<HelmetFeedback> selectAllHelmetFeedback(HelmetFeedback helmetFeedback, String startTime, String endTime, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        return helmetFeedbackMapper.selectAllHelmetFeedback(helmetFeedback,startTime,endTime);
    }

    @Override
    public Integer rowCount(HelmetFeedback helmetFeedback, String startTime, String endTime) {
        return helmetFeedbackMapper.rowCount(helmetFeedback,startTime,endTime);
    }

    @Override
    public int deleteAll(List<String> list) {
        return helmetFeedbackMapper.deleteAll(list);
    }

    @Override
    public String addImg(MultipartFile file) throws IOException {
        String path2 = fileTools.saveFile("feed",file);
        return path2;
    }
}
